import LogFactory from "../log/LogFactory.js";
import { IDENTITY_MAP as map} from "./constant.js";

const log = LogFactory.getLogger("ToastHandler");
/**
 * 全局消息提示处理类
 */
export default class ToastHandler {
    toastLiveExample = document.getElementById('liveToast');
    toast = new bootstrap.Toast(this.toastLiveExample);

    toastLandlordName = $("#toastLandlordName");
    toastWinner = $("#toastWinner");


    init() {
        this.toastLiveExample = document.getElementById('liveToast');
        this.toast = new bootstrap.Toast(this.toastLiveExample);
        this.toastLiveExample.addEventListener('hidden.bs.toast', this.toastCloseCallback.bind(this));
        // log.debug("全局消息对象初始化", map);
    }


    /**
     * 发送全局消息
     * @param {*} data 消息内容 
     * @param {*} callback 消息发送完成后回调函数
     */
    show(data) {
        log.debug("显示全局消息提示信息: ", data);
        let msg = "";

        if(data.chang > 0){
            msg += map.get("CHANG") + "  + " + data.chang /100 + "元  "
        }
        if(data.ma > 0){
            if(msg.length > 0){
                msg += "，"
            }
            msg += map.get("MA") + "  + " + data.ma /100 + "元  "
        }
        if(data.jun > 0){
            if(msg.length > 0){
                msg += "，"
            }
            msg += map.get("JUN") + "  + " + data.jun /100 + "元  "
        }
        this.toastLandlordName.text(map.get(data.landlord));
        this.toastWinner.text(msg);
        this.toast.show();
    }

    /**
     * 消息提示信息关闭回调
     */
    toastCloseCallback() {
        // log.debug("消息提示显示结束回调");
        this.clearMessageContent();
    }

    clearMessageContent() {
        // log.debug("清除消息组件上下文信息");
        this.toastLandlordName.text("");
        this.toastWinner.text("");
    }
}